// CRM Orb.cpp : Defines the entry point for the console application.
//

#include "ICE_Implementation.h"
#include "anyoption.h"
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <Ice/Ice.h>

using namespace CustomerRelationsManagement;
using namespace std;



class CRM_Orb: virtual public Ice::Application
{
public:
	virtual int run(int argc, char* argv[])
	{
		// AnyOption is our command line options parser
		AnyOption *opt = new AnyOption();

		// setup the usage/help
		opt->addUsage("");
		opt->addUsage("Usage: ");
		opt->addUsage("");
		opt->addUsage(" -h --help           Print this help ");
		opt->addUsage(" -dbhost hostname    Set mysql server hostname");
		opt->addUsage(" -dbport 3306		Set the mysql server port");
		opt->addUsage(" -dbuser username    Set the mysql username");
		opt->addUsage(" -dbpass password    Set the mysql password");
		opt->addUsage(" -dbname name		Set the mysql database");
		opt->addUsage(" -IceID              Set the ICE identifer");
		opt->addUsage("");
		opt->addUsage(" -dbhost -dbuser and -dbpass are mandatory");
		opt->addUsage(" -dbport defaults to 3306");
		opt->addUsage(" -dbname defaults to \"CRM\"");
		opt->addUsage(" -IceID defaults to \"CRM\"");
	
		// actually setup the flags now
		opt->setFlag("help", 'h');
		opt->setOption("dbhost");
		opt->setOption("dbport");
		opt->setOption("dbuser");
		opt->setOption("dbpass");
		opt->setOption("dbname");
		opt->setOption("IceID");

		// process the command line
		opt->processFile("./serveropts.cfg");
		opt->processCommandArgs(argc, argv);

		// we've made it this far... time to use those options
		string dbhost, dbuser, dbpass, IceID = "CRM", dbname = "CRM";
		int dbport = 3306;

		// if they're either asked for help or failed to setup the required options then
		// here is the help output

		if (
			(opt->getFlag("help") || opt->getFlag('h')) ||
			!(opt->getValue("dbhost") && opt->getValue("dbuser") && opt->getValue("dbpass"))
			)
		{
			opt->printUsage();
			delete opt;
			return 1;
		}
		dbhost = opt->getValue("dbhost");
		dbuser = opt->getValue("dbuser");
		dbpass = opt->getValue("dbpass");

		if (opt->getValue("IceID"))
			IceID = opt->getValue("IceID");

		if (opt->getValue("dbname"))
			dbname = opt->getValue("dbname");
		

		if (opt->getValue("dbport"))
		{
			from_string<int>(dbport, opt->getValue("dbport"), std::dec);
		}

		// we're done with AnyOption.. clean it up
		delete opt;
		// we're all ready.. lets do this
		MYSQL mysql_conn;
		mysql_init(&mysql_conn);

		if (!mysql_real_connect(&mysql_conn, dbhost.c_str(), dbuser.c_str(),
			dbpass.c_str(), dbname.c_str(), dbport, NULL, 0))
		{
			cout << mysql_error(&mysql_conn) << "(errno=" << mysql_errno(&mysql_conn) << ")" <<  endl;
			return 1;
		}

		// we're connected to mysql, startup our ice ORB
		Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapterWithEndpoints(
				"CRMAdapter", "default -p 31337");
		Ice::ObjectPtr object = new CRM_imp(mysql_conn);
		adapter->add(object, communicator()->stringToIdentity("CRM"));
		adapter->activate();
		
		communicator()->waitForShutdown();
		return 0;
	}
};

int main(int argc, char* argv[])
{
	CRM_Orb orb;
	return orb.main(argc, argv);
}

std::string escape_string(std::string str)
{
	char *tmp = new char[2*str.length()+1];
	mysql_escape_string(tmp, str.c_str(), (unsigned long)str.length());
	std::string retval = tmp;
	delete tmp;
	return retval;
}